<template>
  <section>
    <el-form ref="myForm" :model="formData" label-width="100px">
      <el-row>
        <el-col :sm="12" :md="8" :lg="6">
          <el-form-item label="班级名称" prop="class_name">
            <el-input v-model="formData.class_name" />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="8" :lg="6">
          <el-form-item label="手机号码" prop="user_mobile">
            <el-input v-model="formData.user_mobile" />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="8" :lg="6">
          <el-form-item label="审核状态" prop="join_status">
            <check-status-select v-model="formData.join_status"></check-status-select>
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="8" :lg="6">
          <el-form-item label="添加时间" prop="creator_time">
            <el-date-picker v-model="formData.creator_time" type="daterange" start-placeholder="开始时间"
              end-placeholder="结束时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-button type="primary" @click.prevent="handleQuery(1)" native-type="submit">查询</el-button>
        <el-button @click="handleReset(myForm)">重置</el-button>
      </el-row>
    </el-form>
    <el-table :data="tableData" border>
      <el-table-column prop="class_name" label="班级名称"> </el-table-column>
      <el-table-column prop="real_name" label="用户姓名"> </el-table-column>
      <el-table-column prop="user_mobile" label="手机号码"> </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <span>{{ getStatus(scope.row.join_status) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="creator_time" label="申请时间" />
      <el-table-column prop="creator_time" label="审核时间" />
      <!-- 操作按钮列 -->
      <el-table-column label="操作">
        <template #default="scope">
          <div class="btn-box">
            <el-button type="primary" 
            plain text
            :disabled="scope.row.join_status == 0"
            @click="handleCheck(true, scope.row)">
              审核通过
            </el-button>
            <el-button type="warning" 
            plain 
            text 
            :disabled="scope.row.join_status != 0"
            @click="handleCheck(false, scope.row)">
              审核不通过
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :currentPage="formData.currentPage" :page-size="formData.pageSize" :page-sizes="[10, 20, 50]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </section>
</template>
<script lang="ts" setup>
import CheckStatusSelect from "./CheckStatusSelect.vue";
import request from "@/utils/request";
import {
  computed,
  ref,
  reactive,
  defineEmits,
  defineExpose,
} from "vue";
import { FormInstance, ElMessageBox } from "element-plus";


// 定义子传父的事件触发器
const emits = defineEmits(["query", "show"]);

// 表单参数
const formData = reactive<any>({});


// 使用computed的setter和getter与查询参数的creator_time_begin和creator_time_end挂钩
formData.creator_time = computed({
  set(val: any) {
    formData.creator_time_begin = val ? val[0] : "";
    formData.creator_time_end = val ? val[1] : "";
  },
  get: () => [formData.creator_time_begin, formData.creator_time_end],
});

// 获取表单元素
const myForm = ref<FormInstance>();

// 表格数据
const tableData = reactive<Array<object>>([]);

// 总行数
const total = ref(0);

/**
 * 将当前表单数据提交进行查询
 */
const handleQuery = (currentPage: number) => {
  formData.currentPage = currentPage;
  formData.pageSize = formData.pageSize || 10;
  request({
    url: "/class/queryCheckList",
    method: "POST",
    data: formData,
  }).then(({ data }) => {
    // 查询成功，将结果数据赋值到tableData中
    if (data.code > 0) {
      // 重置表格数据
      tableData.length = 0;
      // 将接口返回的数据数组，添加到表格数据中，tableData是响应式数据，表格组件会自动更新表格数据
      tableData.push(...data.data.list);
      // 将接口返回的总行数，赋值到total中，total传递到分页器组件中，会自动更新分页器组件中的数据
      total.value = data.data.total;
    }
  });
};

/**
 * 当前页变化
 * @param currentPage 当前页
 */
const handleCurrentChange = (currentPage: number) => {
  handleQuery(currentPage);
};

/**
 * 每页行数变化
 * @param pageSize 每页行数
 */
const handleSizeChange = (pageSize: number) => {
  formData.pageSize = pageSize;
  handleQuery(1);
};

/**
 * 点击重置按钮：
 * 清空表单，完成数据查询
 */
const handleReset = (myForm: FormInstance | undefined) => {
  if (myForm) {
    myForm.resetFields();
  }
  handleQuery(1);
};

/**
 * 点击审核按钮，显示审核页面
 */
const handleCheck = (pass: boolean, rowData: any) => {
  const tipe = pass ? '请填写审核意见' : '请填写拒绝意见';
  const type = pass ? 'success' : 'warning';
  const status = pass ? '0' : '7';
  ElMessageBox.prompt(`申请理由：${rowData.reason || '无'}`, tipe, {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    inputValidator(text: string) {
      if (text.length > 100) {
        return "请输入100字以内";
      }
      return true;
    },
    type,
  }).then(({ value }) => {
    request({
      url: "/class/check",
      data: { status, 
        opinion: value, 
        class_id: rowData.class_id,
        user_id: rowData.user_id },
      method: "POST"}).then(res => {
        if (res.data.code > 0) {
          alert("操作成功")
          handleReset(myForm.value);
        } else {
          alert("操作失败")
        }
      })
  });
};

/**
 * 获取状态
 * 加入状态：0=正常 1=审核中 7=被否决
 * @param status 状态
 */
const getStatus = (status: string) => {
  switch (status) {
    case "0":
      return "审核通过";
    case "1":
      return "等待审核";
    case "7":
      return "审核不通过";
  }
};

handleReset(undefined);

// 将重置方法暴露给父组件
defineExpose({ handleReset });
</script>
<style lang="scss" scoped>
.el-table {
  margin-top: 15px;
  height: calc(100vh - 275px);
  width: 100%;

  .image {
    height: 100px;
    width: auto;
  }

  .btn-box {
    display: flex;
    justify-content: space-between;
  }
}
</style>